package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TIaiminfo;
import com.zmht.beike.po.TImonthinfo;
import com.zmht.beike.po.TIweekcontent;
import com.zmht.specedu.vo.Teacher;
/**
 * 个别周主题内容
 * @author Administrator
 *
 */
public class IWeekContentAction extends BaseAction {
	/**
	 * 获得周主题内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getIWeekcontent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.getIweekContentBiz();
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		TIaiminfo iaiminfo = (TIaiminfo)request.getSession().getAttribute("iaiminfo");
		TImonthinfo imonthinfo = (TImonthinfo)request.getSession().getAttribute("imonthinfo");
		
		String week  = request.getParameter("week");
		String cmonthId = request.getParameter("cmonthId");
		
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		String weekName="";
		int total = this.getIweekContentBiz().getIweekcountTotal(teacher.getTeacherId()+"",iaiminfo.getId(),imonthinfo.getMonthType(),new Integer(week));
		List<TIweekcontent> list = this.getIweekContentBiz().getIweekcontent(teacher.getTeacherId()+"",iaiminfo.getId(),imonthinfo.getMonthType(),new Integer(week), (index-1)*pageSize, pageSize);
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		
		request.setAttribute("weekcontents", list);
		request.setAttribute("week", week);
		request.setAttribute("cmonthId", cmonthId);
		if(list!=null&&list.size()>0){
			weekName = list.get(0).getWeekThemeName();
		}
		request.setAttribute("weekThemeName", weekName);
		
		return mapping.findForward("gebiezhou_content");
	}
	/**
	 * 添加周主题内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveIWeekcontent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		TIaiminfo iaiminfo = (TIaiminfo)request.getSession().getAttribute("iaiminfo");
		TImonthinfo imonthinfo = (TImonthinfo)request.getSession().getAttribute("imonthinfo");
		
		System.out.println("添加。。。啊啊啊啊啊啊啊。。。。。。。。。。。");
		String iweekContent = request.getParameter("iweekContent");
		String week = request.getParameter("week");
		String cmonthId = request.getParameter("cmonthId");
		String weekThemeName = request.getParameter("weekThemeName");
		
		TIweekcontent weekcontent = new TIweekcontent();
		weekcontent.setWeekId(new Integer(week));
		weekcontent.setWeekContent(iweekContent);
		weekcontent.setTeacherId(teacher.getTeacherId());
		//weekcontent.setWeekContent(weekThemeName);
		if(null!=imonthinfo){
			weekcontent.setMonth(imonthinfo.getMonthType());
		}
		if(null!=iaiminfo){
			weekcontent.setIaimId(iaiminfo.getId());
		}
		weekcontent.setWeekThemeName(weekThemeName);
		
		if("1".equals(request.getParameter("edit"))){
			String month = request.getParameter("month");
			String targetId = request.getParameter("targetId");
			weekcontent.setIaimId(new Integer(targetId));
			weekcontent.setMonth(new Integer(month));
		}
		
		this.getIweekContentBiz().saveIWeekContent(weekcontent);
		
		request.setAttribute("weekThemeName", weekThemeName);
		if("1".equals(request.getParameter("edit"))){
			String month = request.getParameter("month");
			String targetId = request.getParameter("targetId");
			System.out.println("添加。。。。。。。。。。。。。。");
			return new ActionForward("/beike/iweekcontent.do?method=getIWeekcontentEdit&targetId"+targetId+"&week="+week+"&month="+month);
		}
		
		return new ActionForward("/beike/iweekcontent.do?method=getIWeekcontent&week="+week+"&cmonthId="+cmonthId);
	}
	/**
	 * 删除周主题内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delIWeekcontent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String week = request.getParameter("week");
		String cmonthId = request.getParameter("cmonthId");
		
			this.getIweekContentBiz().deleteIWeekContent(new Integer(id));
	
			if("1".equals(request.getParameter("edit"))){
				String month = request.getParameter("month");
				String targetId = request.getParameter("targetId");
				
				return new ActionForward("/beike/iweekcontent.do?method=getIWeekcontentEdit&targetId"+targetId+"&week="+week+"&month="+month);
			}
		
		return new ActionForward("/beike/iweekcontent.do?method=getIWeekcontent&week="+week+"&cmonthId="+cmonthId);
	}
	/**
	 * 更新周主题内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateIWeekcontent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("cid");
		String week = request.getParameter("week");
		String cmonthId = request.getParameter("cmonthId");
		String iweekContent = request.getParameter("iweekContent");
		TIweekcontent weekcontent = this.getIweekContentBiz().findById(new Integer(id));
		if(null!=weekcontent){
			weekcontent.setWeekContent(iweekContent);
			this.getIweekContentBiz().updateIWeekContent(weekcontent);
		}
		if("1".equals(request.getParameter("edit"))){
			String month = request.getParameter("month");
			String targetId = request.getParameter("targetId");
			
			return new ActionForward("/beike/iweekcontent.do?method=getIWeekcontentEdit&targetId"+targetId+"&week="+week+"&month="+month);
		}
		
		
		return new ActionForward("/beike/iweekcontent.do?method=getIWeekcontent&week="+week+"&cmonthId="+cmonthId);
	}
	/**
	 * 用于编辑时 获得周内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getIWeekcontentEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.getIweekContentBiz();
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String targetId = request.getParameter("targetId");
		String month = request.getParameter("month");
		//TIaiminfo iaiminfo = this.getIaiminfoBiz().findById(new Integer(targetId));
		
		//TImonthinfo imonthinfo = (TImonthinfo)request.getSession().getAttribute("imonthinfo");
		
		String week  = request.getParameter("week");
		//String cmonthId = request.getParameter("cmonthId");
		
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		String weekName="";
		//int total = this.getIweekContentBiz().getIweekcountTotal(teacher.getTeacherId()+"",iaiminfo.getId(),imonthinfo.getMonthType(),new Integer(week));
		//List<TIweekcontent> list = this.getIweekContentBiz().getIweekcontent(teacher.getTeacherId()+"",iaiminfo.getId(),imonthinfo.getMonthType(),new Integer(week), (index-1)*pageSize, pageSize);
		
		int total = this.getIweekContentBiz().getIweekcontentEditTotal(new Integer(targetId), teacher.getTeacherId(), new Integer(month), week);
		List<TIweekcontent> list = this.getIweekContentBiz().getIweekcontentEdit(new Integer(targetId), teacher.getTeacherId(), new Integer(month), week, (index-1)*pageSize, pageSize);
		
		
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		
		request.setAttribute("weekcontents", list);
		request.setAttribute("week", week);
		//request.setAttribute("cmonthId", cmonthId);
		if(list!=null&&list.size()>0){
			weekName = list.get(0).getWeekThemeName();
		}
		request.setAttribute("weekThemeName", weekName);
		
		
		request.setAttribute("targetId", targetId);
		request.setAttribute("month", month);
		request.setAttribute("edit", "1");
		return mapping.findForward("gebiezhou_content");
	}
}
